
var username = "";


var page =1;//默认页数
var pageSize = 10;//每页条数
var total = 0;//总条数
//分页方法
function paging(page,pageSize,total){
	page = Number(page);
	
	//最大页数
	var maxPage = Math.ceil(total/pageSize) || 1;
	if(page>maxPage){
		//取值不能超过maxPage
	    page = Math.min(page,maxPage);
	    tiaozhuan(param,page,pageSize,username);
	}else if(page<1){
		//取值不能小于1
	    page = Math.max(page,1);
	    tiaozhuan(param,page,pageSize,username);
	}else{
		//	当前页数
	$('.ui-paging-count').val(page);
	//首页的置灰处理
	$('.js-page-first').removeClass('ui-pager-disabled');
	//上一页
	$('.js-page-prev').removeClass('ui-pager-disabled').attr("data-page",page-1);
	//下一页
	$('.js-page-next').removeClass('ui-pager-disabled').attr("data-page",page+1);
	//最末页
	$('.js-page-last').removeClass('ui-pager-disabled').attr("data-page",maxPage);
	//总页数显示
	$('.js-page-total').text(maxPage);
	
	
	//当前显示多少页的处理
	$(".ui-select-pagesize").val(pageSize);
	//第一页的处理
	if(page == 1){
		$(".js-page-first").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-prev").addClass('ui-pager-disabled').attr("data-page","");
	}
	if(page == maxPage){
		//最后一页的处理
		$(".js-page-last").addClass('ui-pager-disabled').attr("data-page","");
		$(".js-page-next").addClass('ui-pager-disabled').attr("data-page","");
	}
	}
	 
	
};
//下拉选择分页条数的操作
$('.ui-select-pagesize').on("change",function(){
	pageSize = $(this).val();	
	tiaozhuan(param,page,pageSize,username);
});

//跳转到X页  blur
$(".ui-paging-count").on("blur",function(){
	page = $(this).val();
	tiaozhuan(param,page,pageSize,username);
});

//上一页，下一页，首页，最末页的跳转
$('.ui-pager').on("click",function(){
	page = $(this).attr("data-page");
	tiaozhuan(param,page,pageSize,username);
});

//角色名查找
$('.clickFunc').on("click",function(){
	username = $('#searchRalename').val();
	tiaozhuan(param,page,pageSize,username);
});

function tiaozhuan(param,page,pageSize,username){
	var html = "";
	method = "post";
	datas = {page:page,pageSize:pageSize,username:username};
	getAgentMsg(param,method,datas);
//	console.log(dataResult);
	
	for(var i=0;i<dataResult.data.rows.length;i++){
		var isOpen = "";
		if(dataResult.data.rows[i].userStatus==1){
			isOpen = "open";
		}
		html += "<tr>";
			html += "<td><div class='switch "+isOpen+"' data-status='"+dataResult.data.rows[i].userStatus+"' data-id='"+dataResult.data.rows[i].id+"'></div></td>";
			html += "<td>"+dataResult.data.rows[i].username+"</td>";
			html += "<td>"+dataResult.data.rows[i].name+"</td>";
			html += "<td><a class='dsp-btn btn-blue' href='javascript:void(0)' data-uid='"+dataResult.data.rows[i].id+"' onclick='getRoleListE(this)'>编辑</a>";
			html += "</td>";
		html += "</tr>";
	}
	$(".roleList").html(html);
	//之后处理分页
	total = dataResult.data.total;
	paging(page,pageSize,total);
	$(".switch ").on("click", function() {
		var userStatus = $(this).attr("data-status");
		if(userStatus == 1) {
			userStatus = 0;
		} else {
			userStatus = 1;
		}
		$(this).attr("data-status", userStatus);
		$(this).toggleClass("open");
		//执行修改状态的ajax
		var param2 = "/sys/updateStatus";
		var method2 = "post";
		var userId = $(this).attr("data-id");

		var datas2 = { userId: userId, userStatus: userStatus};
		getAgentMsg2(param2, method2, datas2);
		layer.msg(dataResult2.msg);

	});
}


//添加新用户 （打开模态框）
function UserModal(){
	$("#loginNameK").show();
	$("#usernameK").show();
	$('.addUser').show();
	$('.updateUser').hide();
	$("#userModal").show();
	$(".modal-title").text("新增员工");
}
//新增用户确定按钮
$('.addUser').on("click",function(){
	var username = $('#loginName').val();//登录名
	var password = $('#password').val();//密码
	var name = $('#name').val();//用户名
	var phone = $('#telephone').val();//手机号
	var mail = $('#email').val();//邮箱
	var roleId = $("#role").attr("data-value");//角色
	if(!username || !password || !name || !phone || !roleId){
		layer.msg("带*都为必填项，请完善用户信息！！");
		return false;
	}
	var param3 = "/agency/save";
	var method3 = "post";
	var datas3 = {username:username,password:password,name:name,phone:phone,mail:mail,roleId:roleId};
	getAgentMsg3(param3,method3,datas3);
//	console.log(dataResult3);
	layer.msg(dataResult3.msg,{time:2*1000},function() {
		window.location.reload();
	});	
});
//编辑员工按钮（打开模态框）
function getRoleListE(obj){
	var datauId = $(obj).attr("data-uid");
	$.ajax({
		url: url + "/agency/editInfo/"+datauId,
		type: "post",
		dataType: 'json',
		
		contentType: "application/json",
		useDefaultXhrHeader: false,
		 beforeSend: function(XMLHttpRequest) {
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
		
			if(result.code == '0') {
//				console.log(result.data);
				
				$("#password").val(result.data.password);
				$("#telephone").val(result.data.phone);
				$("#email").val(result.data.mail);
				
				$("#role").attr({"data-value":result.data.role.roleId,"data-uid":datauId}).text(result.data.role.roleName);
				
				
				$("#loginNameK").hide();
				$("#usernameK").hide();
				$('.addUser').hide();
				$('.updateUser').show();
				$("#userModal").show();
				$(".modal-title").text("编辑员工");
				
//				layer.msg('退出成功',{time:2*1000},function() {
//					location.href='login.html';
//				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
	
}
//编辑用户确定按钮
$('.updateUser').on("click",function(){
	var password = $('#password').val();//密码
	var phone = $('#telephone').val();//手机号
	var mail = $('#email').val();//邮箱
	var roleId = $("#role").attr("data-value");//角色
	var userId = $("#role").attr("data-uid");//uid
	if(!password || !phone || !roleId){
		layer.msg("带*都为必填项，请完善用户信息！！");
	}
	var param3 = "/agency/edit";
	var method3 = "post";
	var datas3 = {mail:mail,password:password,phone:phone,roleId:roleId,userId:userId};
	getAgentMsg3(param3,method3,datas3);
	layer.msg(dataResult3.msg,{time:2*1000},function() {
		window.location.reload();
	});	
});
//关闭模态框
$('.close2').on("click",function(){
	$("#userModal").hide();
});

//获取员工角色列表
function getRoleList(){
	$.ajax({
		url: url + "/agency/roleList/0",
		type: "post",
		//		参数需转为字符串
		//		json跨域
		dataType: 'json',
		//		contentType类型为json
		contentType: "application/json",
		useDefaultXhrHeader: false,
		//		请求头加上X-Token 
		 beforeSend: function(XMLHttpRequest) {
			//		token 为页面所获取
			XMLHttpRequest.setRequestHeader("X-Token", token); 
		},
		success: function(result) {
			
			if(result.code == '0') {
				var htmlr = "";													
//				console.log(result.data);
				for(var i=0;i<result.data.length;i++){
					htmlr += '<li class="cl">';
						htmlr += '<a href="javascript:void(0)" data-roleId="'+result.data[i].roleId+'">'+result.data[i].roleName+'</a>';
					htmlr += '</li>';	
				}
				$(".dropdown-menu").html(htmlr);
				
				//下拉选择获取角色id,名称
				$('.cl').on("click",function(){
					$("#role").attr("data-value",$(this).children().attr("data-roleId")).text($(this).children().text());
				});
			} else {
				isErr(result);
			}
		},
		err: function(err) {
			layer.msg("请求失败");
		}
	});
}


//初始化加载数据
window.onload = function(){
	param = "/agency/findUserTypeThree";
	tiaozhuan(param,page,pageSize,username);
	getRoleList();
}